<template>
  <div class="inside-page">
    <div class="user content">
      <p class="detail-title">我的简历</p>
      <div class="com" style="min-height: 400px">
        <!-- <People :info="info.resume" :edu-result="dic.eduResult" :city="dic.city" @setAvatar="setAvatar" /> -->
        <People
          :info="info.resume"
          :edu-result="dic.eduResult"
          :city="dic.city"
        />
      </div>
      <div class="com" style="min-height: 100px">
        <Edu
          :edu-list="info.resume_edu"
          :edu-result="dic.eduResult"
          :qualification-list="dic.edu"
        />
      </div>
      <div class="com" style="min-height: 140px">
        <Work :work-list="info.resume_word" />
      </div>
      <div class="com" style="min-height: 140px">
        <Project :project-list="info.resume_pro" />
      </div>
      <div class="com" style="min-height: 120px">
        <Pos :poss-list="info.resume_school" />
      </div>
      <div class="com" style="min-height: 120px">
        <Reward
          :reward-list="info.resume_school_prize"
          :prize="dic.prize_level"
        />
      </div>
      <div class="com" style="min-height: 150px">
        <Activity :activity-list="info.resume_school_shaky" />
      </div>
      <div class="com" style="min-height: 120px">
        <Cert :cert-list="info.resume_skill" />
      </div>
      <div class="com" style="min-height: 120px">
        <!-- <Annex :annex-list="info.resume_school_file" @setAnnex="setAnnex" /> -->
        <Annex :annex-list="info.resume_school_file" />
      </div>
    </div>
    <div class="submit">
      <button class="confirm" type="button" @click="submit">发布</button>
      <button class="cancel" type="button" @click="cancel">取消</button>
    </div>
  </div>
</template>

<script>
import { loadUser, updateResume } from '@/api/users';
export default {
  asyncData: ({ req }) => ({
  }),
  components: {},
  data () {
    return {
      info: {
        resume: {
          truename: '',
          email: '',
          sex: null,
          birthday_cn: {
            year: '',
            month: '',
            day: '',
          },
          avatar_img: '',
          hopecid: '',
          other_name: '',
          oldcid: '',
          desc: '',
          politic: '',
          politic_cn: '',
          in_school_result: '',
          in_school_result_cn: '',
          oldcid_cn: { name: '' },
          hopecity_cn: { name: '' },
        },
        resume_edu: [],
        resume_pro: [],
        resume_school: [],
        resume_school_prize: [],
        resume_school_shaky: [],
        resume_skill: [],
        resume_word: [],
        resume_school_file: [],
      },
      dic: {
        city: [],
        edu: [],
        prize_level: [],
        eduResult: [],
        politicResult: []
      },
      // avatar: '',
      // annexList: [],
    }
  },
  head () {
    return {
      titleTemplate: (titleChunk) => {
        return `${titleChunk} - 大学生找工作、找实习`;
      }
    }
  },
  created () {
    this.toLoadUser();
  },
  mounted () {
  },
  watch: {
  },
  computed: {

  },
  methods: {
    async toLoadUser () {
      await loadUser({}).then(res => {
        if (res.code === 200) {
          if (res.data) {
            const obj = res.data.user_resume;
            if (res.data.user_resume) {
              obj.resume && obj.resume.hopecid === 0 && (obj.resume.hopecity_cn = { name: '其他' });
              obj.resume && (this.info.resume = obj.resume);
              obj.resume_edu && (this.info.resume_edu = obj.resume_edu);
              obj.resume_pro && (this.info.resume_pro = obj.resume_pro);
              obj.resume_school && (this.info.resume_school = obj.resume_school);
              obj.resume_school_prize && (this.info.resume_school_prize = obj.resume_school_prize);
              obj.resume_school_shaky && (this.info.resume_school_shaky = obj.resume_school_shaky);
              obj.resume_skill && (this.info.resume_skill = obj.resume_skill);
              obj.resume_word && (this.info.resume_word = obj.resume_word);
              obj.resume_school_file && (this.info.resume_school_file = obj.resume_school_file);
            }
            if (res.data.public_array) {
              if (res.data.public_array.city) {
                res.data.public_array.city.push({ id: 0, name: '其他' })
              }
              this.dic = res.data.public_array
            }
          };
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err || '网络错误！')
      })
    },
    cancel () {
      this.$router.go(-1)
    },
    // setAvatar (imgFile) {
    //   this.avatar = imgFile;
    // },
    // setAnnex (list) {
    // this.annexList = list;
    // },
    submit () {
      const uid = this.$store.state.user.authUser.id;

      if (!uid) {
        this.$message.error('未获取到用ID,请重新登录！')
        return
      }
      // FORM
      const formData = new FormData();
      // 技能证书
      const skillList = this.info.resume_skill.map(s => s.skill_id || s.parent_id);
      const skill_ids = skillList.length > 0 ? skillList.join(',') : '';
      // 基本信息
      const { avatar_img, truename, sex, hopecid, oldcid, other_name, phone, email, desc, in_school_result, birthday_cn, politic } = this.info.resume;
      let resume_objParams = {
        avatar_img,
        truename,
        sex,
        oldcid,
        hopecid,
        birthday: birthday_cn,
        in_school_result,
        phone,
        email,
        desc,
        skill_ids,
        other_name,
        politic
      };
      resume_objParams = JSON.stringify(resume_objParams);
      formData.append('uid', uid);
      formData.append('resume', resume_objParams);
      // formData.append('avatar_img', this.avatar);
      // const fileIdList = [];
      // let count = 0;
      // this.annexList.length > 0 && this.annexList.forEach((f, i) => {
      //   if (f.id) { // 已上传文件
      //     fileIdList.push(f.id);
      //   } else { // 新文件
      //     formData.append(`file[${count++}]`, f.file);
      //   }
      // });
      // fileIdList.forEach(s => {
      //   formData.append('file_ids', s);
      // });

      // 教育经历
      // eslint-disable-next-line no-unreachable
      const eduObj = this.info.resume_edu.map(edu => {
        return {
          school: edu.school,
          specialty: edu.specialty,
          seid: edu.seid,
          schoolresult: edu.schoolresult,
          start_time: edu.start_time,
          end_time: edu.end_time,
        }
      }) || [];
      const resume_edu = JSON.stringify(eduObj);
      formData.append('edu', resume_edu);
      // 工作经历
      const wordObj = this.info.resume_word.map(word => {
        return {
          comname: word.comname,
          job: word.job,
          wdesc: word.wdesc,
          start_time: word.start_time,
          end_time: word.end_time,
        }
      }) || [];
      const resume_word = JSON.stringify(wordObj);
      formData.append('work', resume_word);
      // 项目经验
      const proObj = this.info.resume_pro.map(pro => {
        return {
          pname: pro.pname,
          post: pro.post,
          prodesc: pro.prodesc,
          start_time: pro.start_time,
          end_time: pro.end_time,
        }
      }) || [];
      const resume_pro = JSON.stringify(proObj);
      formData.append('pro', resume_pro);
      // 校内职务
      const schoolObj = this.info.resume_school.map(school => {
        return {
          post: school.post,
          edu_name: school.edu_name,
          desc: school.desc,
          start_time: school.start_time,
          end_time: school.end_time,
        }
      }) || [];
      const resume_school = JSON.stringify(schoolObj);
      formData.append('schoolPro', resume_school);
      // 校内奖励
      const schoolPrizeObj = this.info.resume_school_prize.map(schoolPrize => {
        return {
          name: schoolPrize.name,
          edu_name: schoolPrize.edu_name,
          start_time: schoolPrize.start_time,
          level_name: schoolPrize.level_name,
          prize_name: schoolPrize.prize_name,
        }
      });
      const resume_school_prize = JSON.stringify(schoolPrizeObj);
      formData.append('prize', resume_school_prize);
      // 校内活动
      const schoolShakyObj = this.info.resume_school_shaky.map(schoolShaky => {
        return {
          title: schoolShaky.title,
          start_time: schoolShaky.start_time,
          end_time: schoolShaky.end_time,
          edu_name: schoolShaky.edu_name,
          desc: schoolShaky.desc,
        }
      }) || [];
      const resume_school_shaky = JSON.stringify(schoolShakyObj);
      formData.append('shaky', resume_school_shaky);
      // 附件
      const schoolFileObj = this.info.resume_school_file.map(s => {
        return { filename: s.filename, filepath: s.filepath }
      }) || [];
      const resume_school_file = JSON.stringify(schoolFileObj);
      formData.append('file', resume_school_file);

      try {
        updateResume(formData, true).then(res => {
          if (res.code === 200) {
            this.showView = true;
            this.$message.success(res.msg || '保存成功！');
          } else {
            this.$message.error(res.msg || '网络错误, 稍后再试！');
          }
        }).catch(err => {
          console.log(err)
          this.$message.error(err || '网络错误, 稍后再试！');
        })
        // eslint-disable-next-line no-unreachable
      } catch (err) {
        console.dir(err)
        this.errors = err.response.data.errors
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.user {
  padding-top: 40px;
  position: relative;
}
</style>
